package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *菜品管理接口实现类
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * 需要操作两个表dish和flavor
     * @param dishDTO
     */
    @Override
    @Transactional//事务管理注解
    public void saveWithFlavor(DishDTO dishDTO) {
        //1.插入一条菜品数据
        Dish dish = new Dish();
        //菜品属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.saveWithFlavor(dish);
        //获取菜品id
        Long dishId = dish.getId();

        //2.插入多条口味数据
        // 2.1假设 dishDTO.getFlavors() 返回一个 Flavor 对象的列表
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() >0 ){
            // 2.3循环为每个 Flavor 设置 dishId
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            //2.4使用真正的批量插入方法
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //1.分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        //2.分页查询结果
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //1.如果菜品在启售中，无法删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //2.如果菜品和套餐关联，不能删除，一个菜品可能和多个套餐管理
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        if (setmealIds != null && setmealIds.size() > 0){
            //2.1 菜品管理套餐，抛出异常信息
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.批量删除菜品数据
        dishMapper.deleteBatch(ids);
        //4.批量删除菜品关联的口味数据
        dishFlavorMapper.deleteByDishId(ids);
    }

    /**
     * 根据id查询菜品和口味数据
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //1.查询菜品数据
        Dish dish = dishMapper.getById(id);

        //2.查询口味数据
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        //3.封装数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * 需要操作多张表，+Transactional
     * 对于口味表，先清除，后添加
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //1.修改菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        Long dishId = dishDTO.getId();
        //2.删除口味数据
        dishFlavorMapper.deleteByDishId1(dishId);

        //2.插入多条口味数据
        // 2.1假设 dishDTO.getFlavors() 返回一个 Flavor 对象的列表
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() >0 ){
            // 2.3循环为每个 Flavor 设置 dishId
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            //2.4使用真正的批量插入方法
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类id条件查询菜品
     * @param dish
     * @return
     */
    @Override
    public List<Dish> list(Dish dish) {

        List<Dish> list = dishMapper.list(dish);
        return list;
    }

    /**
     * 根据分类id查询菜品X口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> dishWithFlavor(Dish dish) {
        //1.用分类ID查询菜品信息--dish 返回ID查询--dish_flavor
        List<Dish> dishList = dishMapper.list(dish);

        //2.遍历list中的菜品信息，查询对应的口味数据
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            //2.1创建dishVO对象,封装dish数据
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //2.2查询口味数据
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            //2.3封装口味数据
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }
}
